package fight 
{
	import fight.model.InfoVO;
	import org.airmvc.Controller;
	
	import vo.GeneralVO;
	
	/**
	 * ...
	 * @author WLDragon 2014-02-28
	 */
	public class FightC extends Controller 
	{
		
		public function FightC() 
		{
			super();
		}
		
		override protected function addListeners():void 
		{
			interest(Nws.FIGHT_SELECT_FORMATION, selectFormation);
			
			receive(FORMAT_COMPLETE, formatComplete);
			receive(FIGHT_OVER, fightOver);
			receive(SHOW_DEFENSE_HEAD,showHead);
		}
		
		private function showHead(isShow:Boolean):void 
		{
			if (isShow)
			{
				broadcast(Nws.GENERAL_SHOW_HEAD,attackSide.id,defenseSide.id);
			}
			else
			{
				broadcast(Nws.GENERAL_SHOW_HEAD,attackSide.id);
			}
		}
		
		private function fightOver(attackInfo:InfoVO,defenseInfo:InfoVO):void 
		{
			var aKillSoliders:int = defenseSide.soliders - (defenseInfo.infantryNum + defenseInfo.bowmanNum + defenseInfo.sowarNum);
			var dKillSoliders:int = attackSide.soliders - (attackInfo.infantryNum + attackInfo.bowmanNum + attackInfo.sowarNum);
			attackSide.strength = attackInfo.strength;
			attackSide.exp += aKillSoliders * 0.02 >> 0;
			//attackSide.level = ;//增加经验后提升等级和武力
			//attackSide.power
			attackSide.soliders -= dKillSoliders;
			
			defenseSide.strength = defenseInfo.strength;
			defenseSide.exp += dKillSoliders * 0.02 >> 0;
			defenseSide.soliders -= aKillSoliders;
			
			broadcast(Nws.FIGHT_OVER);
		}
		
		private function formatComplete():void 
		{
			Msg.instance.show("");
			broadcast(Nws.MENU_SHOW, 0);
		}
		
		private function selectFormation(type:int):void 
		{
			send(INIT_FORMATION,type,attackSide,defenseSide);
		}
		
		override protected function startup(... args):void 
		{
			//初始化视图
			if (view == null)
			{
				view = new FightV();
				registerView(view);
			}
			
			attackSide = args[1];
			defenseSide = args[2];
			
			//args[3];地形 1-山 2-水 3-平原 4-森林 5-沙漠 6-城池 789村庄
			send(INIT_MAP,args[3]);
		}
		
		/**攻方*/
		private var attackSide:GeneralVO;
		/**守方*/
		private var defenseSide:GeneralVO;
		
		
		/**初始化地图*/
		static public const INIT_MAP:String = "init_map";
		/**初始化阵型*/
		static public const INIT_FORMATION:String = "init_formation";
		/**布阵完成*/
		static public const FORMAT_COMPLETE:String = "format_complete";
		/**下达命令0-前进 1-包围 2-待机 3-后退*/
		static public const GIVE_ORDER:String = "give_order";
		/**打开操作面板*/
		static public const OPEN_CONTROL_PANEL:String = "open_control_panel";
		/**武将逃跑或死亡*/
		static public const LEADER_ESCAPE_OR_DIE:String = "leader_escape_or_die";
		/**战斗结束*/
		static public const FIGHT_OVER:String = "fight_over";
		/**更新信息面板的数据*/
		static public const UPDATE_INFO_PANEL:String = "update_info_panel";
		/**显示防御方头像*/
		static public const SHOW_DEFENSE_HEAD:String = "show_defense_head";
	}
	
}